C++ samples
Common Tasks
Return multiple values
Return multiple values
Algorithms
Copy a range of elements
Copy a range of elements
Copy elements from a range to another range or container.
Count occurrences of value in a range
Count occurrences of value in a range
Count the number of occurrences of a particular value in a range of elements.
Sort a range of elements
Sort a range of elements
Sort elements in a range into a given order.
Swap containers
Swap containers
Swap the contents of two containers.
Swap values
Swap values
Swap the values of two objects.
Classes
Copy-and-swap
Copy-and-swap
Implement the assignment operator with strong exception safety.
Delegate behavior to derived classes
Delegate behavior to derived classes
Delegate behavior to derived classes without incurring the cost of run-time polymorphism.
Lexicographic ordering
Lexicographic ordering
Implement a lexicographic ordering over class members.
Non-member non-friend interfaces
Non-member non-friend interfaces
Reduce dependencies on internal class details and improve encapsulation.
The PIMPL idiom
The PIMPL idiom
Remove compilation dependencies on internal class implementations and improve compile times.
The rule of five
The rule of five
Safely and efficiently implement RAII to encapsulate the management of dynamically allocated resources.
The rule of zero
The rule of zero
Utilise the value semantics of existing types to avoid having to implement custom copy and move operations.
Virtual constructor
Virtual constructor
Create a copy of an object through a pointer to its base type.
Concurrency
Create a thread
Create a thread
Execute code on a separate thread.
Execute a task asynchronously
Execute a task asynchronously
High-level asynchronous execution of tasks.
Pass values between threads
Pass values between threads
Use promises to communicate values between threads.
Containers
Check existence of a key
Check existence of a key
Check if a particular key is in an associative container.
Remove elements from a container
Remove elements from a container
Use the erase-remove idiom to remove elements from a container.
Functions
Apply tuple to a function
Apply tuple to a function
Unpack a tuple as the arguments of a function.
Optional arguments
Optional arguments
Allow argument values to be omitted when calling a function.
Pass arrays
Pass arrays
Pass fixed-size arrays to and from functions.
Return multiple values
Return multiple values
Return multiple values of different types from a function.
Input streams
Read line-by-line
Read line-by-line
Process the contents of an input stream line-by-line.
Read a line of values
Read a line of values
Read a sequence of delimited values from a single line of an input stream into a standard container.
Validate multiple reads
Validate multiple reads
Ensure that multiple stream reads are successful before using the extracted values.
Memory management
Shared ownership
Shared ownership
Share ownership of a dynamically allocated object with another unit of code.
Unique ownership
Unique ownership
Transfer unique ownership of a dynamically allocated object to another unit of code.
Use RAII types
Use RAII types
Avoid manual memory management to improve safety and reduce bugs and memory leaks.
Weak reference
Weak reference
Maintain a non-owning reference to a shared dynamically allocated object to break circular dependencies.
Output streams
Overload operator
Overload operator
Write your class type objects to an output stream.
Write data in columns
Write data in columns
Align data in columns when writing to an output stream.
Random number generation
Choose a random element
Choose a random element
Choose a random element from a container.
Flip a biased coin
Flip a biased coin
Generate a random boolean value according to a bernoulli distribution.
Roll a die
Roll a die
Generate a random integer according to a uniform distribution.
Unpredictable random numbers
Unpredictable random numbers
Seed a random number engine with greater unpredictability.
Ranges
Range-based algorithms
Range-based algorithms
Implement algorithms that can be applied to any generic range of
elements.
Range iteration
Range iteration
Iterate over a range of elements without using iterators or indices.
Templates
Class template SFINAE
Class template SFINAE
Conditionally instantiate a class template depending on the template arguments.
Function template SFINAE
Function template SFINAE
Conditionally instantiate a function template depending on the template arguments.
Perfect forwarding
Perfect forwarding
Forward arguments of one function to another as though the wrapped function had been called directly.
Time
Fixed time step
Fixed time step
Block the execution of a thread until a fixed point in time.
Measure execution time
Measure execution time
Measure the execution time of a unit of code.
Sleep
Sleep
Block the execution of a thread for a given amount of time.
Behavioral
Observer
Observer
Notify generic observer objects when an event occurs.
Visitor
Visitor
Separate generic algorithms from the elements or structure on which they operate.
Creational
Builder
Builder
Separate the complex construction of an object from its representation.
Structural
Decorator
Decorator
Extend the functionality of a class.